package alogrithm;

import java.util.Arrays;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/9/9 20:54
 */
public class InsertSort {

    public static void main(String[] args) {
        int[] a = new int[]{1, 7, 5, 6, 4, 2, 3};
        insertSort(a);
        System.out.println(Arrays.toString(a));
    }

    private static void insertSort(int[] a) {
        int n = a.length;
        for (int i = 1; i < n; ++i) {
            for (int j = i; j < n && a[j] < a[j - 1]; j--) {
                swap(a, j, j - 1);
            }
        }
    }

    private static void swap(int[] a, int j, int i) {
        int temp = a[j];
        a[j] = a[i];
        a[i] = temp;
    }
}
